package cn.lyl.sys.controller;

import cn.lyl.basic.util.AjaxResult;
import cn.lyl.basic.util.PageList;
import cn.lyl.sys.domain.DictionaryType;
import cn.lyl.sys.query.DictionaryTypeQuery;
import cn.lyl.sys.service.IDictionaryDetailService;
import cn.lyl.sys.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口:部门
 */
@Api(value = "部门接口类",description = "处理部门的CRUD操作")
@RestController
@RequestMapping("/dictionaryType")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;
    /**
     * 查找一个
     * @return
     */
        //前端：get http://localhost:8080/type/1
    @ApiOperation(value = "查找单个对象")
    @GetMapping("/{id}")
    public DictionaryType findOne(@PathVariable("id")Long id){
          return dictionaryTypeService.findOne(id);
    }

    /**
     * 查找所有
     * @return
     */
    ////前端：get http://localhost:8080/type
    @ApiOperation(value = "查找所有")
    @GetMapping
    public List<DictionaryType> findAll(){
           return dictionaryTypeService.findAll();
    }

    /**
     * 删除功能
     * @param id
     * @return
     */
    //前端：delete http://localhost:8080/type/1
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            dictionaryDetailService.DelFindById(id);
            dictionaryTypeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或修改
     * @param type
     * @return
     */
    //前端：put http://localhost:8080/type
    @ApiOperation(value = "添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryType type){
        try {
            if(type.getId()==null){
                dictionaryTypeService.add(type);
            }else{
                dictionaryTypeService.update(type);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 分页查询或高级查询
     * @param typeQuery
     * @return
     */
    ////前端：post http://localhost:8080/type json对象{}
    @ApiOperation(value = "分页查询或高级查询")
    @PostMapping
    public PageList<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery typeQuery){
           return dictionaryTypeService.queryPage(typeQuery);
    }

    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            for(Long id : ids){
                dictionaryDetailService.DelFindById(id);
            }
            dictionaryTypeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }



}
